// we7/pages/index/1-1首页2定位版本2.js
var QQMapWX = require('../../resource/utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lat:'',
    lng:'',
    positionNow:'',
    longitude:'',
    latitude:'',
    nearShop:[],
    block:0,
    moRenShop:'',
    searchContent:"",
    shop_id:"",
    idx: 0,
    shoping:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    
    var that = this;
    if (options.shoping){
      console.log(options.shoping)
      that.setData({
        shoping: options.shoping
      })
    }
    
    const value = wx.getStorageSync('openid')
    wx.setNavigationBarTitle({
      title: '选择门店'
    })
   qqmapsdk = new QQMapWX({
      key: '4B7BZ-LD46U-C7FVV-4ZFNG-YVZT3-7MFGJ' // 必填
    });  
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        app.util.request({
          url: 'admin/WxGoodsApi/nearby_store',
          data: {
            uid: value,
            longitude: latitude,
            latitude: longitude
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res){
            if(res.data.code==0){
              that.setData({
                nearShop: res.data.data,
                longitude: latitude,
                latitude: longitude
              })
              var data = res.data.data;
              for(var c = 0; c < data.length; c++){
                if (data[c].default_shop === 1){
                  that.setData({
                    block: c,
                    idx: data[c]['id']
                  })
                }
              }
              if (that.data.default_shop === 0) {
                that.setData({
                  block: 0,
                  idx: data[0]['id']
                })
              }
            }
          }
        });
        that.qqmap(latitude + ',' + longitude)
      }
    })
  },

  qqmap(location){
    var _this = this;
    qqmapsdk.reverseGeocoder({
      location: location || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
      success: function (res) {
        var lat = res.result.location.lat
        var lng = res.result.location.lng
        
        _this.setData({
          positionNow: res.result.address,
          lat: lat,
          lng: lng
        })
      }
    })
  },
  
  // 点击每一个店铺列表
  nearCheck:function(data){
    const value = wx.getStorageSync('openid')
    var that = this;
    var index = data.currentTarget.dataset.id;
    // 选中的门店的id
    var shop_id = that.data.nearShop[index].id
    // console.log(shop_id)
    that.setData({
      block: index,
    })
    app.util.request({
      url: 'admin/WxGoodsApi/selection',
      method: 'post',
      data: {
        shop_id: shop_id,
        uid: value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res){
        if(res.data.code==0){
          if(that.data.shoping){
            wx.reLaunch({
              url: '../shoping/shoping'
            })
          }else{
            wx.reLaunch({
              url: './index'
            })
          }
          
        }
      }
    });
  },
  // 搜索框输入的内容
  searchIcon: function (data){
    this.setData({
      searchContent: data.detail.value
    })

  },
  // 搜索关键字
  searchbin: function(){
    const value = wx.getStorageSync('openid')
    var that = this;
    if(!that.data.searchContent){
      wx.getLocation({ 
        type: 'wgs84',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          app.util.request({
            url: 'admin/WxGoodsApi/nearby_store',
            data: {
              uid: value,
              longitude: latitude,
              latitude: longitude
            },
            method: 'post',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              if (res.data.code == 0) {
                that.setData({
                  nearShop: res.data.data,
                  longitude: latitude,
                  latitude: longitude
                })
                var data = res.data.data;
                for (var c = 0; c < data.length; c++) {
                  if (data[c].default_shop === 1) {
                    that.setData({
                      block: c,
                      idx: data[c]['id']
                    })
                  }
                }
                if (that.data.default_shop === 0) {
                  that.setData({
                    block: 0,
                    idx: data[0]['id']
                  })
                }
              }
            }
          });
          that.qqmap(latitude + ',' + longitude)
        }
      })
    }
    app.util.request({
      url: 'admin/WxGoodsApi/search_store',
      method: 'post',
      data: {
        search_name: that.data.searchContent,
        longitude: that.data.longitude,
        latitude: that.data.latitude
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res){
        if (res.data.code === 1){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          return false;
        }
        var data = res.data.data;
        var str = '';
        for(var i=0;i<data.length;i++){
          if(data[i]['id'] === that.data.idx){
            that.setData({
              block:i,
              idx: data[i]['id']
            })
          }
          str += data[i]['id']+',';
        }
        str = str.slice(0, str.length - 1);
        var arr = str.split(",");
        for(var e=0;e<arr.length;e++){
          arr[e] = parseInt(arr[e]);
        }
        var len = arr.length;
        if (arr.indexOf(that.data.idx) === -1){
          that.setData({
            block: len
          })
        }
        that.setData({
          nearShop: res.data.data
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    const value = wx.getStorageSync('openid')
    const that = this;
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    const data = currPage.data.localhost;
    if (data === undefined){
      return false;
    }
    if (data.address == '') {
      qqmapsdk = new QQMapWX({ key: 'KJHBZ-PMX6X-MXE44-TRKVY-LC6YQ-JYBTM' });
      qqmapsdk.reverseGeocoder({
        location: data.latitude + ',' + data.longitude,
        success: function (res) {
          console.log(res);
          that.setData({
            origin: res.result.address,
            origin_latitude: data.latitude,
            origin_longitude: data.longitude
          })
        }
      })
    } else {
      console.log(data.address);
      that.setData({
        origin: data.address,
        origin_latitude: data.latitude,
        origin_longitude: data.longitude
      })
    }

    // var that = this;
    // var datas = app.globalData.location;
    // qqmapsdk.reverseGeocoder({
    //   location: datas.latitude + ',' + datas.longitude,
    //   success: function (res) {
    //     var lat = res.result.location.lat
    //     var lng = res.result.location.lng
    //     that.setData({
    //       positionNow: res.result.address,
    //       lat: lat,
    //       lng: lng
    //     })
    //   }
    // })
    // if (!datas.latitude){
    //   return false;
    // }
    // app.util.request({
    //   url: 'admin/WxGoodsApi/nearby_store',
    //   data: {
    //     uid: value,
    //     longitude: datas.latitude,
    //     latitude: datas.longitude
    //   },
    //   method: 'post',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   success: function(res){
    //     that.setData({
    //       nearShop: res.data.data,
    //       longitude: datas.latitude,
    //       latitude: datas.longitude
    //     })
    //     var data = res.data.data;
    //     for (var c = 0; c < data.length; c++) {
    //       if (data[c].default_shop === 1) {
    //         that.setData({
    //           block: c
    //         })
    //       }
    //     }
    //     if (that.data.default_shop === 0) {
    //       that.setData({
    //         block: 0
    //       })
    //     }
    //   }
    // });
    //that.qqmap(datas.latitude + ',' + datas.longitude);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})